class ResMgmt {
	static self: ResMgmt;


    /**************************** 单例模式构建 ****************************/

    static create() {
        !ResMgmt.self ? ResMgmt.self = new ResMgmt() : "";
        return ResMgmt.self;
    }


	/********************************* 通过RES资源管理器创建一个位图对象 ***********************************/
	
    static createBitmapByName(name: string): egret.Bitmap {
        let result: egret.Bitmap = new egret.Bitmap();
        let texture: egret.Texture = RES.getRes(name);
        result.texture = texture;
        return result;
    }
	

	private resUrl: string;				//配置文件路径
	private resRoot: string;			//资源根路径
	private resGroupName: string;		//资源组名
	private onComplete: Function;		//资源加载完成回调
	private onProgress: Function;		//资源加载进度回调
	private onGroupError: Function;		//资源组加载错误回调
	private onItemError: Function;		//资源项加载错误回调


	/**************************** 更新资源 ****************************/

	public update(
		res: {
			url: string,
			groupName: string,
			root: string
		},
		onComplete?: Function,
		onProgress?: Function,
		onGroupError?: Function,
		onItemError?: Function
	) {
		//更新资源配置
		this.resUrl = res.url;
		this.resRoot = res.root;
		this.resGroupName = res.groupName;

		//更新回调事件
		this.onComplete = onComplete;
		this.onProgress = onProgress;
		this.onGroupError = onGroupError;
		this.onItemError = onItemError;
		
		//执行资源组更新
		this.loadConfig();
	}


	/**************************** 载入资源配置 ****************************/

	private loadConfig(): void {

		//载入资源配置
		if (this.resUrl && this.resRoot && this.resGroupName) {
			RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        	RES.loadConfig(this.resUrl, this.resRoot);
		}
		
		//场景不含资源，则直接完成回调
		else {
			let onComplete: Function = this.onComplete;
		
			//清空所有回调事件
			this.onProgress = null;
			this.onComplete = null;
			this.onGroupError = null;
			this.onItemError = null;

			//执行完成回调
			onComplete ? onComplete() : "";
		}
	}


	/**************************** 配置文件加载完成，开始预加载资源组。 ****************************/

    private onConfigComplete(): void {
        RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
        RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
        RES.loadGroup(this.resGroupName);
    }


	/**************************** 资源组加载进度 ****************************/

    private onResourceProgress(event: RES.ResourceEvent): void {
		// this.onProgress ? this.onProgress(event.itemsLoaded, event.itemsTotal) 
		// 	: console.log(`已完成：${Math.floor(event.itemsLoaded / event.itemsTotal * 100)}%`);
		this.onProgress ? this.onProgress(event.itemsLoaded, event.itemsTotal) : "";
    }


	/**************************** 资源组加载完成 ****************************/

    private onResourceLoadComplete(event: RES.ResourceEvent): void {
		RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
		RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
		RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
		RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);

		let onComplete: Function = this.onComplete;
		
		//清空所有回调事件
		this.onProgress = null;
		this.onComplete = null;
		this.onGroupError = null;
		this.onItemError = null;

		//执行资源载入完成回调
		onComplete ? onComplete() : console.log("资源加载完成！");
    }


	/**************************** 资源组加载出错 ****************************/

    private onResourceLoadError(event: RES.ResourceEvent): void {
		this.onGroupError ? this.onGroupError(event.groupName) 
			: console.warn(`资源组：${event.groupName} 加载失败！`);
        //忽略加载失败的项目
        this.onResourceLoadComplete(event);
    }


    /**************************** 资源项加载出错 ****************************/

    private onItemLoadError(event: RES.ResourceEvent): void {
		this.onItemError ? this.onItemError(event.resItem.url) 
			: console.warn(`资源项：${event.resItem.url} 加载失败！`);
    }
}